package ru.dadle8;

import org.apache.kafka.clients.producer.*;

public class AsyncSampleProducer extends AbstractProducer {

    @Override
    public void sendMessages(String topicName) {
        Producer<String, String> producer = new KafkaProducer<>(getProperties());
        for (int i = 0; i < 10; i++) {
            producer.send(new ProducerRecord<>(topicName, Integer.toString(i), "async: " + i), new MyProducerCallback());
        }

        System.out.println("Message sent successfully");
        producer.close();
    }

    private class MyProducerCallback implements Callback {

        @Override
        public void onCompletion(RecordMetadata recordMetadata, Exception e) {
            if (e == null) {
                System.out.println("message was sent: " + recordMetadata.offset());
            } else {
                System.out.println("error: " + e.getCause());
            }
        }
    }
}
